import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild, OnInit } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { SupplierInput, ProfileServiceProxy, SupplierModelDto, DataItemServiceProxy, SupplierServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData, OrganizationUnitsTreeComponent } from '../../shared/organization-unit-tree.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { SelectItem } from 'primeng/api';

@Component({
  selector: 'createOrEditSupplierModal',
  templateUrl: './create-or-edit-supplier.component.html',
  styles: [`.user-edit-dialog-profile-image {
    margin-bottom: 20px;
    }`
  ]
})
export class CreateOrEditSupplierComponent extends AppComponentBase implements OnInit {

  @ViewChild('createOrEditModal') modal: ModalDirective;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  supplier: SupplierModelDto = new SupplierModelDto();
  active = false;
  saving = false;
  SupplierClassAyy: SelectItem[];
  SupplierLevelAyy: SelectItem[];
  SecretAyy = [];
  SecretLevelAyy = [];

  selectSecret: number;
  selectClass: string;
  selectLevel: string;
  selectSecretLevel: string;
  constructor(injector: Injector,
    private _supplierService: SupplierServiceProxy,
    private _dataItemAppServiceProxy: DataItemServiceProxy,
    private _profileService: ProfileServiceProxy) {
    super(injector);
  }

  ngOnInit(): void {
    console.log("ngOnInit");
    this.SecretAyy = [
      { label: '否', value: 0 },
      { label: '是', value: 1 }
    ];
    // this.selectSecret = this.SecretAyy[0].value;
    this._dataItemAppServiceProxy.getDataItemDetailListByItemTypeName('合格供方分类').subscribe(resultdata => {
      this.SupplierClassAyy = resultdata;
      this.supplier.supplierClass = this.SupplierClassAyy[0].value;
    });
    this._dataItemAppServiceProxy.getDataItemDetailListByItemTypeName('合格供方分级').subscribe(resultdata => {
      this.SupplierLevelAyy = resultdata;
      this.supplier.supplierLevel = this.SupplierLevelAyy[0].value;
    });
    this._dataItemAppServiceProxy.getDataItemDetailListByItemTypeName('保密资质').subscribe(resultdata => {
      this.SecretLevelAyy = resultdata;
      this.supplier.secretLevel = this.SupplierLevelAyy[0].value;
    });
  }

  show(supplierId?: number): void {
    if (!supplierId) {
      this.active = true;
    }

    this._supplierService.getSupplierForEdit(supplierId).subscribe(resultdata => {
      this.supplier = resultdata;
      this.active = true;
      this.modal.show();
    });
  }

  onShown(): void {
    document.getElementById('DisplayName').focus();
  }

  secretchange(env): void {
    if (env.value == 0)
      this.supplier.secretLevel = '无';
  }

  save(): void {
    let input = new SupplierInput();
    // this.supplier.secret = this.selectSecret;
    // this.supplier.supplierClass = this.selectClass;
    // this.supplier.supplierLevel = this.selectLevel;
    // this.supplier.secretLevel = this.selectSecretLevel;
    input.dto = this.supplier;
    this.saving = true;

    console.log(input);
    this._supplierService.createOrUpdateSupplier(input)
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(() => {
        this.notify.info(this.l('SavedSuccessfully'));
        this.close();
        this.modalSave.emit(null);
      });
  }

  close(): void {
    this.active = false;
    this.modal.hide();
  }


}
